import { useIntersectionObserver } from "@vueuse/core";

// 定义懒加载插件
export const lazyPlugin = {
  install(app) {
    // 来加载指令逻辑
    app.directive("img-lazy", {
      mounted(el, binding) {
        // el 指令绑定的那个元素
        // binding: binding.value
        // console.log(el, binding.value);
        const { stop } = useIntersectionObserver(
          el,
          ([{ isIntersecting }], observerElement) => {
            // console.log(isIntersecting);
            if (isIntersecting) {
              // 进入视图区域
              el.src = binding.value;
              stop();
            }
          }
        );
      },
    });
  },
};
